package repo

import (
	"gorm.io/gorm"
	"sensi/app/model"
)

type Dial struct {
}

func (Dial) DialsByBroadcast(db *gorm.DB, broadcast string) ([]*model.Dial, error) {
	dials := make([]*model.Dial, 0)

	err := db.Where("INSTR(?, broadcast)", broadcast).Find(&dials).Error
	if err == gorm.ErrRecordNotFound {
		return dials, nil
	}
	return dials, err
}

func (Dial) Dials(db *gorm.DB) ([]*model.Dial, error) {
	dials := make([]*model.Dial, 0)

	err := db.Find(&dials).Error
	if err == gorm.ErrRecordNotFound {
		return dials, nil
	}
	return dials, err
}

func (Dial) GetByID(db *gorm.DB, id int) (*model.Dial, error) {
	dial := &model.Dial{}
	err := db.First(dial, id).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}
	return dial, err
}
